package com.boolib.animate.framework.web;


import com.boolib.animate.commons.Consts;
import com.boolib.animate.commons.base.BaseController;
import com.boolib.animate.commons.exception.UploadFileException;
import com.boolib.animate.framework.entity.AnimateWork;
import com.boolib.animate.framework.entity.QueryWork;
import com.boolib.animate.framework.service.advice.AnimateWorkService;
import com.boolib.animate.framework.service.base.impl.BaseAnimateWorkService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.fileupload.FileUploadException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;


import java.io.IOException;

/**
 * @author nier
 * @create 2020-02-20 16:31
 */
@Slf4j
@RequestMapping("/api/work")
@RestController
public class AnimateWorkController  extends BaseController<AnimateWork> {


    @Autowired
    private AnimateWorkService animateWorkService;

    @Autowired
    private BaseAnimateWorkService baseAnimateWorkService;

   @PostMapping
    public AnimateWork save(AnimateWork animateWork) {
        try {
            return animateWorkService.newAnimateWork(animateWork);
        } catch (IOException |FileUploadException e) {
            e.printStackTrace();
            throw new UploadFileException("上传失败");
        }
    }


    @GetMapping("{id}")
    public AnimateWork get(@PathVariable("id") Long id ) {
        final AnimateWork animateWork = new AnimateWork();
        animateWork.setId(id);
        animateWork.setIsDel(Consts.ENABLE);
        animateWork.setShowStatus(Consts.ENABLE);
        return baseAnimateWorkService.queryOneThrow(animateWork);
    }

    @GetMapping("update")
    public PageInfo<AnimateWork> queryUpdateList(@RequestParam(value = "page",defaultValue = "0") Integer page, @RequestParam(value = "size",defaultValue = "10") Integer size){

       Example example = new Example(AnimateWork.class);
        example.createCriteria().andEqualTo("showStatus",0).andEqualTo("isDel",0);
        example.orderBy("updateTime").desc();

        return  animateWorkService.queryWork(example,page,size);

    }

    @GetMapping("jp")
    public PageInfo<AnimateWork> queryJpList(@RequestParam(value = "page",defaultValue = "0") Integer page, @RequestParam(value = "size",defaultValue = "10") Integer size){
        Example example = new Example(AnimateWork.class);
        example.createCriteria().andEqualTo("searchLocationId",2).andEqualTo("showStatus",0).andEqualTo("isDel",0);
        example.orderBy("updateTime").desc();
        return  animateWorkService.queryWork(example,page,size);

    }

    @GetMapping("sort")
    public PageInfo<AnimateWork> querySortList(@RequestParam(value = "page",defaultValue = "0") Integer page, @RequestParam(value = "size",defaultValue = "10") Integer size){
        Example example = new Example(AnimateWork.class);
        example.createCriteria() .andEqualTo("showStatus",0).andEqualTo("isDel",0);
        example.orderBy("collectCount").desc();
        return  animateWorkService.queryWork(example,page,size);

    }

    @GetMapping("score")
    public PageInfo<AnimateWork> queryScoreList(@RequestParam(value = "page",defaultValue = "0") Integer page, @RequestParam(value = "size",defaultValue = "10") Integer size){
        Example example = new Example(AnimateWork.class);
        example.createCriteria() .andEqualTo("showStatus",0).andEqualTo("isDel",0);
        example.orderBy("score").desc();
        return  animateWorkService.queryWork(example,page,size);

    }

    @GetMapping("hot")
    public PageInfo<AnimateWork> queryHotList(@RequestParam(value = "page",defaultValue = "0") Integer page, @RequestParam(value = "size",defaultValue = "10") Integer size){
        log.info(page.toString());
        Example example = new Example(AnimateWork.class);
        example.createCriteria() .andEqualTo("showStatus",0).andEqualTo("isDel",0);
        example.orderBy("popularCount").desc();
        return  animateWorkService.queryWork(example,page,size);

    }


    @GetMapping("recommend")
    public PageInfo<AnimateWork> queryRecommendList(@RequestParam(value = "page",defaultValue = "0") Integer page, @RequestParam(value = "size",defaultValue = "10") Integer size){
        Example example = new Example(AnimateWork.class);
        example.createCriteria() .andEqualTo("showStatus",0).andEqualTo("isDel",0).andEqualTo("isRecommend",1);
        return  animateWorkService.queryWork(example,page,size);
    }

    @GetMapping("search")
    public PageInfo<QueryWork> querySearchList(
            @RequestParam(value = "tag",defaultValue = "-1")Integer tag,
            @RequestParam(value = "location",defaultValue = "-1")Integer location,
            @RequestParam(value = "update",defaultValue = "-1")Integer update,
            @RequestParam(value = "face",defaultValue = "-1")Integer face,
            @RequestParam(value = "sort",defaultValue = "0") Integer sort,
            @RequestParam(value = "page",defaultValue = "0") Integer page, @RequestParam(value = "size",defaultValue = "10") Integer size
    ){

        return  animateWorkService.queryWorkTag(tag,location,update,face,sort,page,size);
    }
}
